//
//  SquareDetailsThread.m
//
//  Created by lianchuang  on 14-11-24
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "SquareDetailsThread.h"


NSString *const kSquareDetailsThreadSubject = @"subject";
NSString *const kSquareDetailsThreadDigest = @"digest";
NSString *const kSquareDetailsThreadMessage = @"message";
NSString *const kSquareDetailsThreadAuthor = @"author";
NSString *const kSquareDetailsThreadHighlight = @"highlight";
NSString *const kSquareDetailsThreadAuthorid = @"authorid";
NSString *const kSquareDetailsThreadPic = @"pic";
NSString *const kSquareDetailsThreadGid = @"gid";
NSString *const kSquareDetailsThreadAvatar = @"avatar";
NSString *const kSquareDetailsThreadDateline = @"dateline";
NSString *const kSquareDetailsThreadIscollection = @"iscollection";
NSString *const kSquareDetailsThreadTid = @"tid";
NSString *const kSquareDetailsThreadIsgood = @"isgood";
NSString *const kSquareDetailsThreadCollection = @"collection";
NSString *const kSquareDetailsThreadGood = @"good";
NSString *const kSquareDetailsThreadReplies = @"replies";
NSString *const kSquareDetailsThreadIscollect = @"iscollect";






@interface SquareDetailsThread ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SquareDetailsThread

@synthesize subject = _subject;
@synthesize digest = _digest;
@synthesize message = _message;
@synthesize author = _author;
@synthesize highlight = _highlight;
@synthesize authorid = _authorid;
@synthesize pic = _pic;
@synthesize gid = _gid;
@synthesize avatar = _avatar;
@synthesize dateline = _dateline;
@synthesize iscollection = _iscollection;
@synthesize tid = _tid;
@synthesize isgood = _isgood;

@synthesize collection = _collection;
@synthesize good = _good;
@synthesize replies = _replies;
@synthesize iscollect = _iscollect;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.subject = [self objectOrNilForKey:kSquareDetailsThreadSubject fromDictionary:dict];
            self.digest = [self objectOrNilForKey:kSquareDetailsThreadDigest fromDictionary:dict];
            self.message = [self objectOrNilForKey:kSquareDetailsThreadMessage fromDictionary:dict];
            self.author = [self objectOrNilForKey:kSquareDetailsThreadAuthor fromDictionary:dict];
            self.highlight = [self objectOrNilForKey:kSquareDetailsThreadHighlight fromDictionary:dict];
            self.authorid = [self objectOrNilForKey:kSquareDetailsThreadAuthorid fromDictionary:dict];
            self.pic = [self objectOrNilForKey:kSquareDetailsThreadPic fromDictionary:dict];
            self.gid = [self objectOrNilForKey:kSquareDetailsThreadGid fromDictionary:dict];
            self.avatar = [self objectOrNilForKey:kSquareDetailsThreadAvatar fromDictionary:dict];
            self.dateline = [self objectOrNilForKey:kSquareDetailsThreadDateline fromDictionary:dict];
            self.iscollection = [[self objectOrNilForKey:kSquareDetailsThreadIscollection fromDictionary:dict] doubleValue];
        self.tid = [self objectOrNilForKey:kSquareDetailsThreadTid fromDictionary:dict];
        self.isgood = [[self objectOrNilForKey:kSquareDetailsThreadIsgood fromDictionary:dict] intValue];
        
        self.collection = [self objectOrNilForKey:kSquareDetailsThreadCollection fromDictionary:dict];
        self.good = [self objectOrNilForKey:kSquareDetailsThreadGood fromDictionary:dict];
        self.replies =[self objectOrNilForKey:kSquareDetailsThreadReplies fromDictionary:dict];
        self.iscollect = [[self objectOrNilForKey:kSquareDetailsThreadIscollect fromDictionary:dict] intValue];



    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.subject forKey:kSquareDetailsThreadSubject];
    [mutableDict setValue:self.digest forKey:kSquareDetailsThreadDigest];
    [mutableDict setValue:self.message forKey:kSquareDetailsThreadMessage];
    [mutableDict setValue:self.author forKey:kSquareDetailsThreadAuthor];
    [mutableDict setValue:self.highlight forKey:kSquareDetailsThreadHighlight];
    [mutableDict setValue:self.authorid forKey:kSquareDetailsThreadAuthorid];
    NSMutableArray *tempArrayForPic = [NSMutableArray array];
    for (NSObject *subArrayObject in self.pic) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForPic addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForPic addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForPic] forKey:kSquareDetailsThreadPic];
    [mutableDict setValue:self.gid forKey:kSquareDetailsThreadGid];
    [mutableDict setValue:self.avatar forKey:kSquareDetailsThreadAvatar];
    [mutableDict setValue:self.dateline forKey:kSquareDetailsThreadDateline];
    [mutableDict setValue:[NSNumber numberWithDouble:self.iscollection] forKey:kSquareDetailsThreadIscollection];
    [mutableDict setValue:self.tid forKey:kSquareDetailsThreadTid];
    [mutableDict setValue:[NSNumber numberWithDouble:self.isgood] forKey:kSquareDetailsThreadIsgood];
    
    [mutableDict setValue:self.collection forKey:kSquareDetailsThreadCollection];
    [mutableDict setValue:self.good forKey:kSquareDetailsThreadGood];
    [mutableDict setValue:self.replies forKey:kSquareDetailsThreadReplies];
    [mutableDict setValue:[NSNumber numberWithInteger:self.iscollect] forKey:kSquareDetailsThreadIscollect];

    



    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.subject = [aDecoder decodeObjectForKey:kSquareDetailsThreadSubject];
    self.digest = [aDecoder decodeObjectForKey:kSquareDetailsThreadDigest];
    self.message = [aDecoder decodeObjectForKey:kSquareDetailsThreadMessage];
    self.author = [aDecoder decodeObjectForKey:kSquareDetailsThreadAuthor];
    self.highlight = [aDecoder decodeObjectForKey:kSquareDetailsThreadHighlight];
    self.authorid = [aDecoder decodeObjectForKey:kSquareDetailsThreadAuthorid];
    self.pic = [aDecoder decodeObjectForKey:kSquareDetailsThreadPic];
    self.gid = [aDecoder decodeObjectForKey:kSquareDetailsThreadGid];
    self.avatar = [aDecoder decodeObjectForKey:kSquareDetailsThreadAvatar];
    self.dateline = [aDecoder decodeObjectForKey:kSquareDetailsThreadDateline];
    self.iscollection = [aDecoder decodeDoubleForKey:kSquareDetailsThreadIscollection];
    self.tid = [aDecoder decodeObjectForKey:kSquareDetailsThreadTid];
    self.isgood = [[aDecoder decodeObjectForKey:kSquareDetailsThreadIsgood] intValue];
    self.collection = [aDecoder decodeObjectForKey:kSquareDetailsThreadCollection];
    self.good = [aDecoder decodeObjectForKey:kSquareDetailsThreadGood];
    self.replies= [aDecoder decodeObjectForKey:kSquareDetailsThreadReplies];
    self.iscollect = [[aDecoder decodeObjectForKey:kSquareDetailsThreadIscollect] intValue];



    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_subject forKey:kSquareDetailsThreadSubject];
    [aCoder encodeObject:_digest forKey:kSquareDetailsThreadDigest];
    [aCoder encodeObject:_message forKey:kSquareDetailsThreadMessage];
    [aCoder encodeObject:_author forKey:kSquareDetailsThreadAuthor];
    [aCoder encodeObject:_highlight forKey:kSquareDetailsThreadHighlight];
    [aCoder encodeObject:_authorid forKey:kSquareDetailsThreadAuthorid];
    [aCoder encodeObject:_pic forKey:kSquareDetailsThreadPic];
    [aCoder encodeObject:_gid forKey:kSquareDetailsThreadGid];
    [aCoder encodeObject:_avatar forKey:kSquareDetailsThreadAvatar];
    [aCoder encodeObject:_dateline forKey:kSquareDetailsThreadDateline];
    [aCoder encodeDouble:_iscollection forKey:kSquareDetailsThreadIscollection];
    [aCoder encodeObject:_tid forKey:kSquareDetailsThreadTid];
    [aCoder encodeDouble:_isgood forKey:kSquareDetailsThreadIsgood];
    [aCoder encodeObject:_collection forKey:kSquareDetailsThreadCollection];
    [aCoder encodeObject:_good forKey:kSquareDetailsThreadGood];
    [aCoder encodeObject:_replies forKey:kSquareDetailsThreadReplies];
    [aCoder encodeInteger:_iscollect forKey:kSquareDetailsThreadIscollect];



}

- (id)copyWithZone:(NSZone *)zone
{
    SquareDetailsThread *copy = [[SquareDetailsThread alloc] init];
    
    if (copy) {

        copy.subject = [self.subject copyWithZone:zone];
        copy.digest = [self.digest copyWithZone:zone];
        copy.message = [self.message copyWithZone:zone];
        copy.author = [self.author copyWithZone:zone];
        copy.highlight = [self.highlight copyWithZone:zone];
        copy.authorid = [self.authorid copyWithZone:zone];
        copy.pic = [self.pic copyWithZone:zone];
        copy.gid = [self.gid copyWithZone:zone];
        copy.avatar = [self.avatar copyWithZone:zone];
        copy.dateline = [self.dateline copyWithZone:zone];
        copy.iscollection = self.iscollection;
        copy.tid = [self.tid copyWithZone:zone];
        copy.isgood = self.isgood;
        
        copy.collection = [self.collection copyWithZone:zone];
        copy.good = [self.good copyWithZone:zone];
        copy.replies = [self.replies copyWithZone:zone];
        copy.iscollect = self.iscollect;


    }
    
    return copy;
}


@end
